package ro.prjmanager.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ro.prjmanager.core.dao.GenericDAO;
import ro.prjmanager.database.DataBaseReader;
import ro.prjmanager.database.model.Table;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static ro.prjmanager.service.SynchInterfaceService.MYSQL;
import static ro.prjmanager.service.SynchInterfaceService.ORACLE;

public class SynchDao extends GenericDAO {

    public static final Log log = LogFactory.getLog(SynchDao.class);

    public List<String> getAllTableFromSchema(String url, String user, String pwd, String schema, int type) {
        List<String> ret = null;
        String sql = "";
        String driver = "";

        switch (type) {
            case MYSQL:
                sql = "SELECT TABLE_NAME FROM information_schema.TABLES where TABLE_SCHEMA='" + schema + "'";
                driver = "com.mysql.jdbc.Driver";
                break;
            case ORACLE:
                sql="select table_name from all_tables where owner in ('"+schema.toUpperCase()+"')";
                 driver = "oracle.jdbc.OracleDriver";
        }
        Connection con = null;
        CallableStatement stm = null;
        try {
            con = new DataBaseReader(driver, url, user, pwd, schema).getCon();
            stm = con.prepareCall(sql);
            ResultSet rs = stm.executeQuery();
            if (rs != null) {
                ret = new ArrayList<String>();
                while (rs.next()) {
                    ret.add(rs.getString(1));
                }
            }
        } catch (Exception e) {
            log.error(e);
        } finally {
            if (stm != null)
                try {
                    stm.close();
                } catch (SQLException e) {
                    log.error(e);
                }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    log.error(e);
                }
            }
        }
        return ret;
    }
    public List<String> getAllTableFromSchema(int type) {
        List<String> ret = null;
        String sql = "";
        String driver = "";
        DataBaseReader dbReader=DataBaseReader.getInstance();

        switch (type) {
            case MYSQL:
                sql = "SELECT TABLE_NAME FROM information_schema.TABLES where TABLE_SCHEMA='" + dbReader.getSchema() + "'";

                break;
            case ORACLE:
                break;
        }
        Connection con = null;
        CallableStatement stm = null;
        try {
            con = dbReader.getCon();
            stm = con.prepareCall(sql);
            ResultSet rs = stm.executeQuery();
            if (rs != null) {
                ret = new ArrayList<String>();
                while (rs.next()) {
                    ret.add(rs.getString(1));
                }
            }
        } catch (Exception e) {
            log.error(e);
        } finally {
            if (stm != null)
                try {
                    stm.close();
                } catch (SQLException e) {
                    log.error(e);
                }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    log.error(e);
                }
            }
        }
        return ret;
    }

    public Table syncTable(String url, String user, String pwd, String schema, int type,String tableName){
        String driver=null;

        switch (type) {
            case MYSQL:
                 driver = "com.mysql.jdbc.Driver";
                break;
            case ORACLE:
                driver="oracle.jdbc.driver.OracleDriver";
                break;
        }
        
        DataBaseReader dbr= DataBaseReader.getInstance(driver, url, user, pwd, schema);
        Table table= dbr.loadTable(tableName,false);
        try {
            dbr.releaseConnection();
        } catch (SQLException e) {
            log.error(e);
        }
        return table;
    }






}
